package me.zhengjie.modules.serviceprovider.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.modules.serviceprovider.domain.TServiceproviderContract;
import me.zhengjie.modules.serviceprovider.service.TServiceproviderContractService;
import me.zhengjie.modules.serviceprovider.service.dto.TServiceproviderContractQueryCriteria;
import me.zhengjie.utils.HttpResponseUtils;
import org.apache.tomcat.util.http.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
* @author xuyukun
* @date 2019-07-20
*/
@RestController
@RequestMapping("api")
public class TServiceproviderContractController {

    @Autowired
    private TServiceproviderContractService tServiceproviderContractService;

    @Log("查询TServiceproviderContract")
    @GetMapping(value = "/tServiceproviderContract")
    @PreAuthorize("hasAnyRole('ADMIN','TSERVICEPROVIDERCONTRACT_ALL','TSERVICEPROVIDERCONTRACT_SELECT')")
    public ResponseEntity getTServiceproviderContracts(TServiceproviderContractQueryCriteria criteria, Pageable pageable){
        try {
            return new ResponseEntity(tServiceproviderContractService.queryAll(criteria, pageable), HttpStatus.OK);
        }catch(Exception ex) {
            ex.printStackTrace();
            return HttpResponseUtils.passDefaultResponse(ex.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @Log("新增TServiceproviderContract")
    @PostMapping(value = "/tServiceproviderContract")
    @PreAuthorize("hasAnyRole('ADMIN','TSERVICEPROVIDERCONTRACT_ALL','TSERVICEPROVIDERCONTRACT_CREATE')")
    public ResponseEntity create(@Validated @RequestBody TServiceproviderContract resources){
        return new ResponseEntity(tServiceproviderContractService.create(resources),HttpStatus.CREATED);
    }

    @Log("修改TServiceproviderContract")
    @PutMapping(value = "/tServiceproviderContract")
    @PreAuthorize("hasAnyRole('ADMIN','TSERVICEPROVIDERCONTRACT_ALL','TSERVICEPROVIDERCONTRACT_EDIT')")
    public ResponseEntity update(@Validated @RequestBody TServiceproviderContract resources){
        tServiceproviderContractService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @Log("删除TServiceproviderContract")
    @DeleteMapping(value = "/tServiceproviderContract/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','TSERVICEPROVIDERCONTRACT_ALL','TSERVICEPROVIDERCONTRACT_DELETE')")
    public ResponseEntity delete(@PathVariable Long id){
        try {
            tServiceproviderContractService.delete(id);
            return new ResponseEntity(HttpStatus.OK);
        } catch (Exception ex) {
            ex.printStackTrace();
            return HttpResponseUtils.passDefaultResponse(ex);
        }
    }
}